package ee.ut.math.tvt.ryhm;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

import org.apache.log4j.Logger;

/**
 * @author Margus Hanni
 *
 */
public class IntroUI extends JFrame {

	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(IntroUI.class);
	private static final String PROP_INTRO_PREFIX = "intro";
	private static final String PROP_INTRO_LOGO = PROP_INTRO_PREFIX + ".logo";
	private static final String PROP_INTRO_LIIKMED = PROP_INTRO_PREFIX + ".liikmed";

	private Properties appProperties = new Properties();
	private Properties verProperties = new Properties();

	public IntroUI() {

		try {
			URL url = ClassLoader.getSystemResource("application.properties");
			appProperties.load(new FileInputStream(new File(url.getFile())));
			url = ClassLoader.getSystemResource("version.properties");
			verProperties.load(new FileInputStream(new File(url.getFile())));
		} catch (Exception e) {
			log.error(e);
		}

		setTitle("Intro");

		// size & location
		int width = 250;
		int height = 450;
		setSize(width, height);
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((screen.width - width) / 2, (screen.height - height) / 2);

		draw();

		log.info("IntroUI started");
	}

	private void draw() {
		JPanel content = (JPanel) getContentPane();
		content.setLayout(new FlowLayout());

		JPanel panel = new JPanel();
		ImageIcon logo = new ImageIcon(getLogo());
		JLabel logoLabel = new JLabel(logo);
		panel.add(logoLabel);
		content.add(panel);

		for (Object obj : appProperties.keySet()) {
			String key = (String) obj;
			if (key.startsWith(PROP_INTRO_PREFIX) && !key.startsWith(PROP_INTRO_LOGO)) {
				panel = new JPanel();
				String showKey = key.replace(PROP_INTRO_PREFIX + ".", "");
				if (key.startsWith(PROP_INTRO_LIIKMED)) {
					JTextArea textArea = new JTextArea(showKey + ": " + appProperties.getProperty(key));
					textArea.setWrapStyleWord(true);
					textArea.setLineWrap(true);
					textArea.setEditable(false);
					panel.add(textArea);
					content.add(panel);
				} else {
					JLabel label = new JLabel(showKey + ": " + appProperties.getProperty(key), SwingConstants.CENTER);
					panel.add(label);
					content.add(panel);
				}
			}
		}
		panel = new JPanel();
		JLabel label = new JLabel("Versioon: " + verProperties.getProperty("build.number"));
		panel.add(label);
		content.add(panel);
	}

	private BufferedImage getLogo() {
		BufferedImage img;
		try {
			img = ImageIO.read(new URL(appProperties.getProperty(PROP_INTRO_LOGO)));
		} catch (IOException ex) {
			log.error(ex);
			return null;
		}

		int w = img.getWidth();
		int h = img.getHeight();

		BufferedImage dimg = new BufferedImage(200, 200, img.getType());

		Graphics2D g = dimg.createGraphics();
		g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g.drawImage(img, 0, 0, 200, 200, 0, 0, w, h, null);
		g.dispose();
		return dimg;
	}
}
